
#include "AttackerRole.h"
#include "../GameState.h"


AttackerRole::AttackerRole(Ship *my_ship) :
    my_ship(my_ship) {

    my_team_id = my_ship->getControllingPlayer()->getTeamId();

    get_behaviour = new GetEnemyFlag(my_ship);
    return_behaviour = new ReturnEnemyFlag(my_ship);
    dog_fight_behaviour = new DogFightBehaviour(my_ship);
}

AttackerRole::~AttackerRole(){
    delete get_behaviour;
    delete return_behaviour;
    delete dog_fight_behaviour;

}

bool AttackerRole::shouldIBeAttacker(void){
    return true;
}

bool AttackerRole::perform(void){
    if(get_behaviour->shouldIPerform()){
        if(!fightCloseEnemies()){
            get_behaviour->perform();
        }
    }
    else if(return_behaviour->shouldIPerform()){
        return_behaviour->perform();
    }
 
    return true;
}


bool AttackerRole::fightCloseEnemies(void){
    std::vector<Player*> enemy_players = 
        GameState::instance().getTeam(1-my_team_id).getPlayers();

    for(unsigned i = 0; i < enemy_players.size(); i++){
        Ship *enemy_ship = enemy_players[i]->getShip();
        if(dog_fight_behaviour->shouldIAttack(enemy_ship)){
            dog_fight_behaviour->attackTarget(enemy_ship);
            return true;
        }
    }
    
    return false;
}

